//
//  XingchengViewController.m
//  Travel
//
//  Created by lanou3g on 15/4/12.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "XingchengViewController.h"
#import <AFNetworking.h>
#import "Xingcheng.h"
#import "XingchengCell.h"
#import <UIImageView+WebCache.h>
#import "Travelnote.h"
#import "XingchengDetailViewController.h"
@interface XingchengViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,assign)NSInteger flag;
@end

@implementation XingchengViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self createrequest];
    
}
- (void)createTableView
{
    self.title = [NSString stringWithFormat:@"%@行程",self.numberName];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (void)createrequest
{
    self.arr = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"https://chanyouji.com/api/destinations/plans/%@.json?page=1",self.numberXingcheng];
    AFHTTPRequestOperationManager *manager  = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary *dic in responseObject) {
            Xingcheng *xin = [[Xingcheng alloc] init];
            [xin setValuesForKeysWithDictionary:dic];
            [self.arr addObject:xin];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"xingcheng";
    XingchengCell *cell = [self.tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[XingchengCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    Xingcheng *xin = [self.arr objectAtIndexedSubscript:indexPath.row];
    [cell.imageViewPhoto sd_setImageWithURL:[NSURL URLWithString:xin.image_url] placeholderImage:nil];
    cell.labelDes.numberOfLines = 0;
    cell.labelDes.font = [UIFont systemFontOfSize:15];
    cell.labelDes.text = xin.DES;
    cell.labelName.font = [UIFont systemFontOfSize:20];
    cell.labelName.textColor = [UIColor whiteColor];
    cell.labelName.textAlignment = NSTextAlignmentLeft;
    cell.labelName.text = xin.name;
    NSString *strA = [NSString stringWithFormat:@"%@天 / %@个旅行地",[xin.plan_days_count stringValue],[xin.plan_nodes_count stringValue]];
    cell.labelNumber.textAlignment = NSTextAlignmentLeft;
    cell.labelNumber.textColor = [UIColor whiteColor];
    cell.labelNumber.text = strA;
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGRect rect = [cell.labelDes.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 40, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.labelDes.frame = CGRectMake(20, 210, self.view.frame.size.width - 40,rect.size.height);
    self.flag = cell.labelDes.frame.size.height + 220;
    [cell.contentView addSubview:cell.labelDes];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Xingcheng *note = [self.arr objectAtIndex:indexPath.row];
    XingchengDetailViewController *detal = [[XingchengDetailViewController alloc]init];
    detal.hidesBottomBarWhenPushed = YES;
    detal.IDNumber = note.ID;
    [self.navigationController pushViewController:detal animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.flag;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
